package cn.kgc.ssm.service.impl;

import cn.kgc.ssm.entity.Member;
import cn.kgc.ssm.entity.Message;
import cn.kgc.ssm.mapper.MessageMapper;
import cn.kgc.ssm.service.MessageService;
import cn.kgc.ssm.utils.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/3/8
 * @description
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private MessageMapper messageMapper;


    @Override
    public ResponseResult publishMessage(Message message, HttpSession session) {

        Member  member = (Member) session.getAttribute("memberInfo");
        message.setMemberName(member.getMemberName());
        message.setTel(member.getTel());

        messageMapper.addMessage(message);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("留言发布成功");

        return responseResult;
    }

    @Override
    public ResponseResult messagePage(Integer page, Integer limit, String memberName) {

        PageHelper.startPage(page,limit);

       List<Message> messages =  messageMapper.messageList(memberName);
        PageInfo<Message> messagePageInfo = new PageInfo<>(messages);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("查询成功");
        responseResult.setCount(messagePageInfo.getTotal());
        responseResult.setData(messages);


        return responseResult;
    }

    @Override
    public ResponseResult deleteMessage(Integer[] ids) {

        messageMapper.deleteMessage(ids);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("删除成功");
        return responseResult;
    }

    @Override
    public ResponseResult editMessageStatus(Integer id) {

        messageMapper.upadteMessageStatus(id);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("回访成功");
        responseResult.setCode(0);
        return responseResult;
    }
}
